package com.chcit.mobile.mvp.model;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.mvp.model.api.service.UserService;
import com.chcit.mobile.mvp.model.entity.ReceiveBean;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.FragmentScope;

import javax.inject.Inject;

import com.chcit.mobile.mvp.contract.ReceiveContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@FragmentScope
public class ReceiveModel extends BaseModel implements ReceiveContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public ReceiveModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<List<ReceiveBean>> getReceives(JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                                    .request(HttpMethodContains.PURCHASE_RECEVIE_QUERY_DETAIL,json)
                                    .map(new Function<JSONObject, List<ReceiveBean>>() {
                                        @Override
                                        public List<ReceiveBean> apply(JSONObject jsonObject) throws Exception {
                                            ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                                            if(resultBean.isSuccess()){
                                                if(resultBean.getData().getInteger("total")==0){
                                                    throw new Exception("没有查询到数据！");
                                                }
                                                return resultBean.getRows(ReceiveBean.class);

                                            }else{
                                                throw new Exception("请求失败: "+resultBean.getMsg());
                                            }

                                        }
                                    });
    }

    @Override
    public Observable<ResultBean> doBatchConfirm(JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                //.receiveLine(json)
                .request(HttpMethodContains.RECEVIE_BATCH_OK,json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                    if(resultBean.isSuccess()){
                        return resultBean;
                    }else{
                        throw new Exception("请求失败: "+resultBean.getMsg());
                    }
                });
    }


}